package view.admin;

import controllers.RoomController;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import entity.*;
public  class ManageType extends JPanel{
	
	private JTable tbType;
	private String[] stuffs = new String[] {"Thêm loại", "Sửa loại", "Xóa loại"};
	private JButton[] btStuffs = new JButton[stuffs.length];
	private ArrayList<entity.RoomType> roomTypes ;
	public ManageType(){
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(300, 400));
		tbType = new JTable();
		tbType.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tbType.setPreferredSize(new Dimension(300, 330));
		tbType.setRowHeight(25);
		tbType.setModel(new model.RoomTypeModel(RoomController.getAllType()));
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment(JLabel.CENTER);
		tbType.getColumnModel().getColumn(1).setCellRenderer(centerRenderer);		
		updateList();		
		JPanel jpStuff = new JPanel(new FlowLayout(FlowLayout.LEFT));
		for (int i=0; i<stuffs.length; i++){
			btStuffs[i] = new JButton(stuffs[i]);
			btStuffs[i].addActionListener(new StuffListener());
			jpStuff.add(btStuffs[i]);
		}		
		add(jpStuff, BorderLayout.NORTH);
		JScrollPane scroll = new JScrollPane(tbType);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		add(scroll, BorderLayout.CENTER);
	}
	
	private void updateList(){
		tbType.repaint();	
	}
	
	public void addType(){
		RoomType roomType = new RoomType();
		TypeOperate to = new TypeOperate((JDialog)SwingUtilities.getRoot(this),
						"Sửa loại phòng", roomType,-1);
		if (to.getResult()){
                        ((model.RoomTypeModel) tbType.getModel()).insertList(to.getRoomType());
			updateList();
                        System.out.println("Thêm loại phòng thành công ");
		}
	}
	
	public void editType(){
		int index = tbType.getSelectedRow();
		if (index <0) return;
                roomTypes = RoomController.getAllType();	
		RoomType roomType = roomTypes.get(index);
		TypeOperate to = new TypeOperate((JDialog)SwingUtilities.getRoot(this),
						"Sửa loại phòng", roomType,roomType.getId());

		if (to.getResult()){
                        ((model.RoomTypeModel) tbType.getModel()).editList(index,to.getRoomType());
			updateList();
                        JOptionPane.showMessageDialog(this,"Sửa loại phòng thành công");
		}
	}
	
	public void removeType(){
		int index = tbType.getSelectedRow();
		if (index <0) return;
                    roomTypes = RoomController.getAllType();	
		 if(RoomController.getAllByType(roomTypes.get(index).getId()).isEmpty()){
                     int choice = JOptionPane.showOptionDialog(null, 
							"Bạn có chắc muốn xóa không?", 
							"Xóa loại phòng", 
							JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 
							null, 
							new String[]{"Xác nhận", "Bỏ qua"}, 0);
                       if (choice == 0){  
                                RoomController.removeType(roomTypes.get(index).getId()); 
                                ((model.RoomTypeModel) tbType.getModel()).removeRow(index); 
                                updateList();
                                JOptionPane.showMessageDialog(null,"Xóa loại phòng thành công");
                          }
                  }else{
                        JOptionPane.showMessageDialog(null,"Tồn tại  các phòng có loại "+roomTypes.get(index).getName());
                  
                 }
		
	}
	
	class StuffListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			int i=0;
			for (i=0; i<btStuffs.length; i++)
				if (event.getSource() == btStuffs[i])
					break;
			switch(i){
			case 0: addType();		break;
			case 1: editType();		break;
			case 2: removeType();           break;
			}
		}
	}
	
	
}
